package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.api.RetrofitUtils
import com.ht.medicaltreatment.bean.*
import com.xgst.driverlesscars.api.ApiService
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class BuildOrderViewModel:BaseViewModel() {
    //查询样本盒
    fun findSample(projectId:String,sampleName:String?,sampleCategoryId:String?
                         ,onSuccess:(MutableList<PDQueryBoxBean>)->Unit){
        viewModelScope.launch {
            val bean = mApi.findSample(projectId,sampleName?:"",sampleCategoryId?:"")
            when(bean){
                is ApiResult.Success -> onSuccess(bean.data.data!!)
                is ApiResult.Error -> {
                    dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    fun reuqestSampleBox(id:String,onSuccess:(PDOrderDetailBean)->Unit){
        showProgress()
        viewModelScope.launch {
            val bean = mApi.getSampleBoxs(id)
            when(bean){
                is ApiResult.Success -> {
                    dismissProgress()
                    onSuccess(bean.data.data!!)
                }
                is ApiResult.Error -> {
                    dismissProgress()
                    ToastUtils.showShort(bean.errorMessage)
                }
            }
        }
    }

    //删除盘点计划中的样本盒
    fun delete(inventorySampleBoxIdList:String,onResult:()->Unit){
        viewModelScope.launch {
            val bean = mApi.deleteFromPlan(inventorySampleBoxIdList)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    fun save(name:String,rfidSampleBoxIdList:String,onResult:()->Unit){
        viewModelScope.launch {
            val bean = mApi.savePDPlan(TimeUtils.getNowString(),name,rfidSampleBoxIdList)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

    //修改
    fun revise(id:String,name:String,rfidSampleBoxIdList:String,onResult:()->Unit){
        viewModelScope.launch {
            val bean = mApi.revisePDPlan(id,name,rfidSampleBoxIdList)
            when(bean){
                is ApiResult.Success -> onResult()
                is ApiResult.Error ->  ToastUtils.showShort(bean.errorMessage)
            }
        }
    }

}